﻿package cn.lite3.notepaper.managers
{
	import cn.lite3.notepaper.events.NotepaperEvent;
	import cn.lite3.notepaper.view.NotepaperWindow;
	import cn.lite3.notepaper.vo.NotepaperVo;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	/**
	 * 有数据更新的时候触发
	 */
	[Event(name="dataChange", type="cn.lite3.notepaper.events.NotepaperEvent")]
	
	public class NotepaperWindowManager extends EventDispatcher
	{
		
		private static var instance:NotepaperWindowManager = null;
		
		/**
		 * 获取NotepaperWindowManager 的唯一实例
		 * @return
		 */
		public static function getInstance():NotepaperWindowManager
		{
			if (!instance) instance = new NotepaperWindowManager();
			return instance;
		}
		
		private const dict:Dictionary = new Dictionary();
		
		private var _alwaysInFrontAll:Boolean = true;
		private var _showCount:int = 0;
		private var _count:int = 0;
		
		
		/** 是否所有notepaerWindow都总在最前端 */
		public function get alwaysInFrontAll():Boolean { return _alwaysInFrontAll; }
		/** 是否所有notepaerWindow都总在最前端 */
		public function set alwaysInFrontAll(value:Boolean):void
		{
			if(_alwaysInFrontAll != value)
			{
				_alwaysInFrontAll = value;
				for each(var window:NotepaperWindow in dict)
				{
					if(window.alwaysInFront != value)
					{
						window.alwaysInFront = value;
					}
				}
			}
		}
		
		/**
		 * notepaerWindow的个数
		 * @return
		 */
		public function getNotepaperWindowCount():int
		{
			return _count;
		}
		
		/**
		 * notepaperWindow显示的个数
		 * @return
		 */
		public function getNotepaperWindowShowCount():int
		{
			return _showCount;
		}
		
		public function getDict():Dictionary
		{
			return dict;
		}
		
		/**
		 * 创建一个窗口
		 * @param	vo	窗口初始化对象
		 * @return
		 */
		public function createWindow(vo:NotepaperVo = null):NotepaperWindow
		{
			var window:NotepaperWindow = new NotepaperWindow();
			
			window.addEventListener(NotepaperEvent.DATA_CHANGE, dataChangeHandler);
			
			dict[window] = window;
			// 初始化
			if (vo != null)
			{
				window.subject = vo.subject;
				window.content = vo.content;
				window.x = vo.x;
				window.y = vo.y;
			}
			
			window.activate();
			_count++;
			_showCount++;
			return window;
		}
		
		private function dataChangeHandler(e:NotepaperEvent):void 
		{
			dispatchEvent(e.clone());
		}
		
		/**
		 * 删除一个窗口
		 * @param	window
		 */
		public function removeWindow(window:NotepaperWindow):void
		{
			if (!dict[window]) return;
			delete dict[window];
			
			_count--;
			if (window.visible) _showCount--;
			window.close();
			
			window.removeEventListener(NotepaperEvent.DATA_CHANGE, dataChangeHandler);
			dispatchEvent(new NotepaperEvent(NotepaperEvent.DATA_CHANGE));
		}
		
		/**
		 * 显示所有notepaer窗口 active
		 */
		public function showAll():void
		{
			changNotepaperDisplay(true);
		}
		
		/**
		 * 隐藏所有notepaer窗口 close
		 */
		public function hideAll():void
		{
			changNotepaperDisplay(false);
		}
		
		/**
		 * 改变notepaer窗口的现实状态
		 * @param	isShow	是否显示
		 */
		private function changNotepaperDisplay(isShow:Boolean):void
		{
			for each(var window:NotepaperWindow in dict)
			{
				if(window.visible != isShow)
				{
					window.visible = isShow;
					isShow ? _showCount++ : _showCount--;
				}
			}
		}
	}
}